package com.siblings.loan.ui.loan.impl

import android.content.Context
import com.siblings.lib_permission.OnPermissionCallback
import com.siblings.lib_permission.XXPermissions
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.http.IHttpCallback
import com.siblings.loan.ui.loan.view.ISibLoanLimitView
import com.siblings.loan.utils.event.SibEventType
import com.siblings.loan.utils.helper.SharedHelper
import com.siblings.mvp.presenter.MvpPresenter
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/4 15:46
 * @Annotation：贷款额度 Presenter 实现
 */
class SibLoanLimitPresenterImpl(context: Context?) : MvpPresenter<ISibLoanLimitView>(context) {
    private var model: SibLoanLimitModelImpl? = null

    //所有权限
    private var allPermissions: ArrayList<String>? = null

    init {
        model = SibLoanLimitModelImpl()
    }

    /**
     * 获取用户贷款额度信息
     */
    fun loadSibLoanLimitAction(){
        model!!.loadSibLoanLimitAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null) {
                            mvpView.showSibLoanLimitInfo(obj)
                        }
                    }
                    else -> {
                        mvpView.showSibToast(data.optString("msg"))
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView.hideSibLoading()
            }
        })
    }

    /**
     * 获取所有权限列表
     */
    fun loadSibAllPermissionsAction() {
        if (allPermissions == null){
            allPermissions = arrayListOf()
        }
        allPermissions!!.clear()
        Thread{
            val commonData = JSONObject(SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_COMMON))
            val customize = commonData.optJSONObject("customize")
            if(customize != null){
                val permissions = customize.optJSONObject("permissions")
                if (permissions != null){
                    val permission = permissions.optJSONArray("permission")
                    if (permission != null && permission.length() > 0){
                        for (i in 0..<permission.length()){
                            allPermissions!!.add(permission.optString(i))
                        }
                    }

                    val home = permissions.optJSONArray("home")
                    if (home != null && home.length() > 0){
                        for (i in 0..<home.length()){
                            val str = home.optString(i)
                            if (allPermissions!!.contains(str)){
                                continue
                            }
                            allPermissions!!.add(str)
                        }
                    }
                }
            }
        }.start()
    }

    /**
     * 检查权限是否全部申请
     */
    fun checkSibAllPermissionsAgree(){
        HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_LOAN_LIMIT_ADD_INFORMATION,"")
        if (XXPermissions.isGranted(mvpView.getContext()!!,allPermissions!!)){
            mvpView.showSibCheckPermissionSuccess()
            return
        }
        XXPermissions.with(mvpView.getContext()!!)
            // 申请多个权限
            .permission(allPermissions!!)
            .request(object: OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, p1: Boolean) {
                    var permissionString = ""
                    permissions.forEach {
                        permissionString = "$permissionString$it,"
                    }
                    HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                    mvpView.showSibCheckPermissionSuccess()
                }
                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    var permissionString = ""
                    permissions.forEach {
                        permissionString = "$permissionString$it,"
                    }
                    HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))

                    XXPermissions.startPermissionActivity(context!!,permissions)
                }
            })
    }

}